package com.hy.fangZhou;

import com.hy.fangZhou.utils.exception.MessageUtil;
import com.hy.fbox.ConsoleLoggerFactory;
import com.hy.fbox.ServerCaller;
import com.hy.fbox.StaticCredentialProvider;
import com.hy.fbox.TokenManager;
import com.hy.fangZhou.iot.FBoxSignalRConnection;
import com.hy.fangZhou.iot.Global;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class AppConfig {


    @Bean(name="fBoxSignalRConnection")
    public static FBoxSignalRConnection newFBoxSignalR() {
        ConsoleLoggerFactory loggerFactory = new ConsoleLoggerFactory();

        // 指定连接服务器的凭据参数
        TokenManager tokenManager = new TokenManager(new StaticCredentialProvider(Global.clientId, Global.clientSecret, Global.username, Global.password), Global.idServerUrl, loggerFactory);

        ServerCaller commServer = new ServerCaller(tokenManager, Global.commServerApiUrl, Global.signalrClientId, loggerFactory);
        ServerCaller appServer = new ServerCaller(tokenManager, Global.appServerApiUrl, Global.signalrClientId, loggerFactory);
        ServerCaller hdataServer = new ServerCaller(tokenManager, Global.hdataServerApiUrl, Global.signalrClientId, loggerFactory);

        Global.commServer = commServer;
        Global.appServer = appServer;
        Global.hdataServer = hdataServer;

        return new FBoxSignalRConnection(Global.commServerSignalRUrl, Global.signalrClientId, tokenManager, Global.proxy, loggerFactory);
    }

    @Bean
    public MessageUtil newMessageUtil() {
        MessageUtil util = new MessageUtil();
        util.setBasenames("messages");
        return util;
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
